package com.neulogistics.contrib.procuredcenter.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateGoodsCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateGoodsListCommand;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.Goods;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.GoodsList;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.GoodsSet;
import com.neulogistics.contrib.procuredcenter.domain.repository.IGoodsListRepository;
import com.neulogistics.contrib.procuredcenter.domain.repository.IGoodsRepository;
import com.neulogistics.contrib.procuredcenter.domain.repository.IGoodsSetRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class UpdateGoodsCommandHandler implements IRequestHandler<UpdateGoodsCommand, BaseResponse> {
    @Resource
    IGoodsSetRepository goodsSetRepository;

    @Resource
    IGoodsRepository goodsRepository;

    @Override
    public BaseResponse handle(UpdateGoodsCommand command) throws Exception {
        GoodsSet goodsSet = goodsSetRepository.findById(command.setId);
        if(goodsSet == null){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }
        Goods goodsToCheck = goodsRepository.findById(command.goodsId);
        if(goodsToCheck == null){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }

        Goods goods = new Goods();

        BeanUtils.copyProperties(command, goods);

        goodsRepository.save(goods);

        return ResultUtils.success(null);
    }
}
